package com.mjkj.demo.controller;

import cn.dev33.satoken.annotation.*;
import cn.dev33.satoken.stp.StpUtil;
import com.mjkj.demo.core.result.Result;
import com.mjkj.demo.core.result.ResultGenerator;
import com.mjkj.demo.model.req.LoginReq;
import com.mjkj.demo.model.req.UserReq;
import com.mjkj.demo.model.vo.LoginVo;
import com.mjkj.demo.model.vo.UserVo;
import com.mjkj.demo.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 用户信息表 前端控制器
 * </p>
 *
 * @author Xudoom
 * @since 2023-10-16
 */
@Slf4j
@RestController
@RequestMapping("/user")
@Api(value = "用户信息",tags = {"用户信息"})
public class UserController {


    @Resource
    private UserService userService;


    @PostMapping("/login")
    @ApiOperation(value="用户登录",notes="用户登录")
    @SaIgnore
    public Result<LoginVo> login(@Valid @RequestBody LoginReq loginReq) {
        return userService.login(loginReq);
    }

    @PostMapping("/logout")
    @ApiOperation(value="退出登录",notes="退出登录")
    @SaCheckLogin
    public Result<String> logout() {
        StpUtil.logout();
        return ResultGenerator.genSuccessResult("退出成功");
    }


    @GetMapping("/list")
    @ApiOperation(value="用户列表",notes="用户列表")
    public Result<List<UserVo>> listUser() {
        List<UserVo> list = userService.listUser();
        return ResultGenerator.genSuccessResult(list);
    }

    @PostMapping("/add")
    @ApiOperation(value="添加用户",notes="添加用户")
    @SaCheckRole(value = {"role2", "admin"}, mode = SaMode.OR)
    public Result addUser(@Valid @RequestBody UserReq userReq) {
        userService.addUser(userReq);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/update")
    @ApiOperation(value="修改角色",notes="修改角色")
    public Result updateUser(@Valid @RequestBody UserReq userReq) {
        userService.updateUser(userReq);
        return ResultGenerator.genSuccessResult();
    }

}
